package com.fone.player.online.activity;

import java.util.ArrayList;
import java.util.zip.Inflater;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.fone.player.R;
import com.fone.player.activity.BaseActivity;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.HotspotRst;
import com.fone.player.client.Request;
import com.fone.player.client.RstSerializer;
import com.fone.player.client.HotspotRst.Module;
import com.fone.player.online.adapter.HotAdapter;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.SharedPreferenceOnline;
import com.fone.player.widget.xlistview.XListView;
import com.fone.player.widget.xlistview.XListView.IXListViewListener;

/**
 * 全网热点Activity。
 */
public class HotActivity extends BaseActivity implements IXListViewListener {
	/** 日志标签 */
	private static final String TAG = "HotFragment";

	private Button mLeftBt;
	private Button mRightBt;
	private TextView mTitleTv;
	private XListView mListView;
	private View footerView;

	private HotAdapter hotAdapter;

	private RstSerializer rstSerializer;
	private SharedPreferenceOnline sp;

	private String shost;
	private String host;
	private String name;

	private ArrayList<Module> moduleList = new ArrayList<Module>();

	private static final int SUCCESS = 0;
	private static final int FAILURE = 1;

	// private boolean isFirst = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hot);

		initView();
		initData();
	}

	private void initView() {

		Intent intent = getIntent();
		name = intent.getStringExtra("name");
		footerView = LayoutInflater.from(this).inflate(R.layout.hot_footer, null);
		mLeftBt = (Button) findViewById(R.id.common_title_left_bt);
		mRightBt = (Button) findViewById(R.id.common_title_right_bt);
		mTitleTv = (TextView) findViewById(R.id.common_left_title_tv);

		mListView = (XListView) findViewById(R.id.list);

		mLeftBt.setOnClickListener(new BtnOnClicker());
		mRightBt.setVisibility(View.GONE);
		mTitleTv.setVisibility(View.VISIBLE);
		mTitleTv.setText(name);

		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(true);
		mListView.setEnableDragLoadMore(false);
		mListView.setXListViewListener(this);
		// mListView.setOnScrollListener(this);

		hotAdapter = new HotAdapter(this, host, shost, moduleList);
		mListView.setAdapter(hotAdapter, true);
		mListView.addFooterView(footerView);
	}

	private void initData() {
		rstSerializer = new RstSerializer();
		sp = new SharedPreferenceOnline(HotActivity.this, "全网热点");

		String data = sp.getData();
		// 当缓存为空时，向服务器申请数据
		if (TextUtils.isEmpty(data)) {
			initHotData();
		} else {
			try {
				HotspotRst t = (HotspotRst) rstSerializer.fromString(HotspotRst.class, data);
				updateHot(true, t);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				initHotData();
				// isFirst = true;
			}
			// 当有网且缓存数据超时，重新申请数据
			if (FoneUtil.isNetOk(HotActivity.this) && isTimeOut(sp.getTime(), System.currentTimeMillis())) {
				initHotData();
				// isFirst = true;
			}
		}
	}

	/**
	 * 判断缓存是否过期
	 */
	private boolean isTimeOut(Long sptime, Long currenttime) {
		long num = (long) ((currenttime - sptime) / 60000);
		return num > 15;
	}

	private void initHotData() {
		// 如果没有网络，则不向服务器申请数据
		if (FoneUtil.isNetOkWithToast(HotActivity.this)) {
			L.v(TAG, "HotCallBack", "initData()");
			Request.getInstance().hotspot(new HotCallBack());
		}

	}

	private class HotCallBack implements Callback<HotspotRst> {

		@Override
		public void onSuccess(HotspotRst t) {
			mListView.stopRefresh();
			updateHot(false, t);
		}

		@Override
		public void onFailure(Error error) {
			L.v(TAG, "HotCallBack", "-onFailure-" + error.toString());
			mListView.stopRefresh();
		}
	}

	private void updateHot(boolean isLocal, HotspotRst t) {
		L.v(TAG, "HotCallBack", "-onSuccess-" + t.result);

		if (t.result == SUCCESS) {

			if (!isLocal) {
				sp.setAll(System.currentTimeMillis(), rstSerializer.toString(t));
			}

			shost = t.shost;
			host = t.host;
			if (t.modules != null && t.modules.moduleList != null && t.modules.moduleList.size() > 0) {
				moduleList.clear();
				moduleList.addAll((ArrayList<Module>) t.modules.moduleList);
			} else {
				return;
			}
			hotAdapter.setData(host, shost, moduleList);

		} else if (t.result == FAILURE) {
			showShortToast(t.error.errormsg);
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		mListView.onLoad();
		if (FoneUtil.isNetOk(HotActivity.this)) {
			Request.getInstance().hotspot(new HotCallBack());
			// isFirst = true;
			// moduleList.clear();
		}
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub

	}

	private class BtnOnClicker implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.common_title_left_bt:
				finish();
				break;

			default:
				break;
			}
		}

	}

}
